$(document).ready(function() {

	var restable = $('#res-table').DataTable({
		"order" : [ [ 2, "desc" ] ],// desc默认排序
		"paging" : true,
		"dom" : '<"top">rt<"bottom"ip><"clear">',
		"ajax" : {
			"url" : basePath + "/myDemandManager/getRespondList.do",
			"type" : "post",
			"data" : function(d) {
				d.id = $("#id").val();
			}
		},
		"serverSide" : "true",
		"processing" : "true",
		"columns" : [ 
			  {data : "regName"}, 
			  {data : "company",       className: "desktop"}, 
			  {data : "createdAt",       className: "desktop"}, 
			  {data : "receiveConfrim"}, 
			  {data : "id"}, 
			  {data : "userId"}, 
			  {data : "createdAtStr"}

		],
		"columnDefs" : [
		      {
					"targets" : 2,
					"data" : null,
					"render" : function(data,type, row) {
						return row.createdAtStr;
					}
		      },
		      {
					"targets" : 3,
					"data" : null,
					"render" : function(data,type, row) {
						return renderConfirm(data);
					}
		      },
		      {
					"targets" : 4,
					"orderable" : false,
					"data" : "download_link",
					"render" : function(data,type, row) {
						
						var temp = '<a class="btn btn-sm {0}" href="javascript:void(0)" onClick=javascript:setConfirm("{1}","{2}")>{3}</a>';
						 
						if (row.receiveConfrim === '3') {
							
							return temp.format("btn-warning",row.id,row.receiveConfrim,'取消');
													
						} else {
							
							return temp.format("btn-info",row.id,row.receiveConfrim,'成交');
						}					

					}
				}, {
					"targets" : 5,
					visible : false
				}, {
					"targets" : 6,
					visible : false
				}

		]
	// end of columnDefs

	}).on('preXhr.dt',function(e, settings, data) {

		//showAppendLoadingImg("tree-title");

	}).on('xhr.dt',function(e, settings, data) {

		//hideLoadingImg();
		//加载完响应列表后，刷新主表中的响应数
		
		if(data && data.status==='success')
		{
			$('#table tr.selected').children('td').eq(3).html("<label>"+data.data.length+"</label>");
		}

	}).on('processing.dt',function(e, settings, processing) {

	}).on('error.dt',function(){
    	window.location.reload();
    });// end of dataTable

	$('#res-table tbody').on('click','tr',function() {
		
		$('#res-table tr.selected').removeClass('selected');
		
		$(this).addClass('selected');
		
	});

});

//设置成交或取消成交
function setConfirm(id, receiveConfirm) {

	if ($('#demand-status').val() === 'chatting') {
		
		if($('#original-to-confirm').val()!=="1")
		{
			myNoty('需求正在洽谈中，且非未确认，不能设置响应成交状态', 'failed');
		
			return;
		}
		
	}
	
	var tConfirm = '3';

	//原为成交，设置为不成交
	if (receiveConfirm === '3') {
		
		tConfirm = '2';
		
	}

	$.myAjax(basePath + '/myDemandManager/setDeal.do',
			{respondId : id,isDeal : tConfirm}, 
			'post', 
			function(data) {

				if (data.status === 10001) {
					myNoty(data.msg, 'success');
					// set status
					// response list and info
					//$('#res-table').DataTable().ajax.reload();
					
					//$('#table').DataTable().ajax.refresh();				
					
					getDemandInfo($('#id').val());
					
		
				} else {
					myNoty(data.msg, 'failed');
				}
		
			}, function() {
				showLoading('loadingResponseImg');
			}, function() {
				hideLoading('loadingResponseImg');
			},function(){
		    	//window.location.reload();
		    }
		);

}

//渲染成交状态
function renderConfirm(data) {
	
	var temp='<input type="hidden" value="{0}"><label class="label {1}">{2}</label>';
	
	if (data === '0') {
		
		return temp.format(data,"label-default","初始"); 
		
	}

	if (data === '1') {
		
		return temp.format(data,"label-info","待确认");
	
	}

	if (data === '2') {
	
		return temp.format(data,"label-warning","未成交");
	}

	if (data === '3') {
	
		return temp.format(data,"label-success","已成交");
	
	}
	if (data === '4') {
		
		return temp.format(data,"label-danger","无人响应");
	
	}
	return temp.format(data,"label-danger","未知");
}
//渲染激活状态
function renderActive(data)
{
	var temp='<input type="hidden" value="{0}"><label class="label {1}">{2}</label>';

	if (data === '0') {
		return temp.format(data,"label-warning","否"); 
	}

	if (data === '1') {
		return temp.format(data,"label-success","是");
	}
	
	return temp.format(data,"label-danger","未知");

}
//渲染需求状态
function renderStatus(data)
{
	var temp='<label class="label {0}">{1}</label>';
	
    if (data === "end") {
        return temp.format("label-info","已结束");
    }
    if (data === "chatting") {
    	return temp.format("label-warning","洽谈中");
        
    }
    if (data === "closed") {
    	return temp.format("label-success","已关闭");                        
    }	
    
    if (data === "check") {
    	return temp.format("label-danger","待审核");                        
    }
    
    return temp.format(data,"label-danger","未知");
}